#include "MonkeyFrameListener.h"

MonkeyFrameListener::MonkeyFrameListener( RenderWindow *win, Camera *cam, SceneNode* sn ) : ExampleFrameListener(win, cam), mNode(sn)
{
	this->win = win;
	mChar = 0;
	mExCamera = 0;
	mMode = 0;
	
	//info_manager = new InfoManager();
}

void MonkeyFrameListener::set_info_manager( InfoManager * info_manager )
{
	this->info_manager = info_manager;
	runner = true;
}

bool MonkeyFrameListener::frameStarted( const FrameEvent& evt )
{
	mKeyboard->capture();
	
	if(runner)
	{
		if (mChar)
		{
			mChar->update (evt.timeSinceLastFrame, mKeyboard, NULL);
			
			if (mExCamera) 
			{
				switch (mMode) 
				{
					case 0: // 3rd person chase
						mExCamera->update (evt.timeSinceLastFrame, 
							mChar->getCameraNode()->getWorldPosition(),
							mChar->getSightNode ()->getWorldPosition ());
						break;
	
					case 1: // 3rd person fixed
						mExCamera->update (evt.timeSinceLastFrame, 
							Vector3 (0, 200, 0), 
							mChar->getSightNode ()->getWorldPosition ());
						break;
	
					case 2: // 1st person
						mExCamera->update (evt.timeSinceLastFrame, 
							mChar->getWorldPosition (), 
							mChar->getSightNode ()->getWorldPosition ());
						break;
				}
			}
		}
	
	
		// 3rd Person - Chase Camera
		if (mKeyboard->isKeyDown (OIS::KC_F1))
		{
			mMode = 0;
			if (mChar)
				static_cast<OgreCharacter *>(mChar)->setVisible (true);
			
			if (mExCamera) 
			{
				if (mChar)
					mExCamera->instantUpdate (mChar->getCameraNode()->getWorldPosition(),
					mChar->getSightNode ()->getWorldPosition ());
				mExCamera->setTightness (0.01f);
			}
		}
	
		// 3rd Person - Fixed Camera
		if (mKeyboard->isKeyDown (OIS::KC_F2))
		{
			mMode = 1;
			if (mChar)
				static_cast<OgreCharacter *>(mChar)->setVisible (true);
			if (mExCamera)
			{
				if (mChar)
					mExCamera->instantUpdate (Vector3 (0, 200, 0),
						mChar->getSightNode ()->getWorldPosition ());
				mExCamera->setTightness (0.01f);
			}
		}
	
		// 1st Person
		if (mKeyboard->isKeyDown (OIS::KC_F3))
		{
			mMode = 2;
			if (mChar)
				static_cast<OgreCharacter *>(mChar)->setVisible (false);
	
			if (mExCamera)
			{
				if (mChar)
					mExCamera->instantUpdate (mChar->getWorldPosition (), 
						mChar->getSightNode ()->getWorldPosition ());
				mExCamera->setTightness (1.0f);
			}
		}
	
	
		/* hacking the enemies */
		enemy_manager->update(mChar->getWorldPosition());
		
		info_manager->update_overlay("EnemyArea", "Enemies: ", enemy_manager->get_enemy_size());
		info_manager->update_overlay("LivesArea", "Lives: ", static_cast<OgreCharacter *>(mChar)->get_lives());
		info_manager->update_overlay("EnemyKillArea", "Kills: ", enemy_manager->get_enemy_kills());
		
		/* creating boost info */
		int boost = static_cast<OgreCharacter *>(mChar)->get_boost();
		float boost_timer = static_cast<OgreCharacter *>(mChar)->get_boost_timer();
		
		string b("Boost: " + info_manager->to_string(boost) + " / Timer: " + info_manager->to_string(boost_timer) );
		info_manager->update_overlay("BoostArea", b, NULL);
		
		/* creating the fps label */
		string f("Cur FPS: " + info_manager->to_string(win->getLastFPS()) + 
			" / Avg FPS:" + info_manager->to_string(win->getAverageFPS()) );
		info_manager->update_overlay("FPSArea", f, NULL);
		
		
		if(!static_cast<OgreCharacter *>(mChar)->is_alive())
		{
			runner = false;
			info_manager->create_new_overlay("GameOverPanel", "GameOverArea", "GameOverOverlay", 200, 200, 56);
			info_manager->update_overlay("GameOverArea", "GAME OVER!!!!", NULL);
		}
			
	}
	
	if(mKeyboard->isKeyDown( OIS::KC_P ))
		runner = false;

	if(mKeyboard->isKeyDown( OIS::KC_O ))
		runner = true;
	
	// Exit if we press Esc
	if(mKeyboard->isKeyDown (OIS::KC_ESCAPE))
		return false;
    
	return true;
}
